﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Data.SQLite;
using System.Collections;

using CRM.SQLiteHelper;
using CRM.Entity;
using CRM.Common;

namespace CRM.DataAccess
{
    public partial class News_Detail_DataAccess
    {
    
        public DictionaryEntry News_Detail_GetParameter_Update(News_Detail news_Detail)
        {
            DictionaryEntry dic = new DictionaryEntry();
            
            StringBuilder sb = new StringBuilder();

            sb.Append("update [News_Detail] set ");
            sb.Append("news_id=@news_id,");
            sb.Append("title=@title,");
            sb.Append("content=@content,");
            sb.Append("disable=@disable,");
            sb.Append("create_time=@create_time,");
            sb.Append("last_modify_time=@last_modify_time");
            sb.Append(" where news_id=@news_id");
            
            SQLiteParameter[] sqlParameter = 
            {
                  new SQLiteParameter("@news_id",news_Detail.news_id),
                  new SQLiteParameter("@title",news_Detail.title),
                  new SQLiteParameter("@content",news_Detail.content),
                  new SQLiteParameter("@disable",news_Detail.disable),
                  new SQLiteParameter("@create_time",news_Detail.create_time),
                  new SQLiteParameter("@last_modify_time",news_Detail.last_modify_time),
            };
            
            dic.Key = sb.ToString();
            dic.Value = sqlParameter;

            return dic;
        }
        
        public DictionaryEntry News_Detail_GetParameter_Add(News_Detail news_Detail)
        {
            DictionaryEntry dic = new DictionaryEntry();
        
            StringBuilder sb = new StringBuilder();

            sb.Append("insert into [News_Detail] ");
            sb.Append("(");
            sb.Append("title,");
            sb.Append("content,");
            sb.Append("disable,");
            sb.Append("create_time,");
            sb.Append("last_modify_time");
            sb.Append(") ");
            sb.Append("values");
            sb.Append(" (");
            sb.Append("@title,");
            sb.Append("@content,");
            sb.Append("@disable,");
            sb.Append("@create_time,");
            sb.Append("@last_modify_time");
           sb.Append(") ;select last_insert_rowid()");

            SQLiteParameter[] sqlParameter = new SQLiteParameter[]
            {
                   new SQLiteParameter("@title",news_Detail.title),
                   new SQLiteParameter("@content",news_Detail.content),
                   new SQLiteParameter("@disable",news_Detail.disable),
                   new SQLiteParameter("@create_time",news_Detail.create_time),
                   new SQLiteParameter("@last_modify_time",news_Detail.last_modify_time),
            };
            
            dic.Key = sb.ToString();
            dic.Value = sqlParameter;

            return dic;
        }
    
        public bool News_Detail_Add(News_Detail news_Detail)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("insert into [News_Detail] ");
            sb.Append("(");
            sb.Append("title,");
            sb.Append("content,");
            sb.Append("disable,");
            sb.Append("create_time,");
            sb.Append("last_modify_time");
            sb.Append(") ");
            sb.Append("values");
            sb.Append(" (");
            sb.Append("@title,");
            sb.Append("@content,");
            sb.Append("@disable,");
            sb.Append("@create_time,");
            sb.Append("@last_modify_time");
           sb.Append(") ;select last_insert_rowid()");

            SQLiteParameter[] sqlParameter = new SQLiteParameter[]
            {
                   new SQLiteParameter("@title",news_Detail.title),
                   new SQLiteParameter("@content",news_Detail.content),
                   new SQLiteParameter("@disable",news_Detail.disable),
                   new SQLiteParameter("@create_time",news_Detail.create_time),
                   new SQLiteParameter("@last_modify_time",news_Detail.last_modify_time),
            };

            try
            {
                int id = Convert.ToInt32(DbHelperSQL.ExecuteScalar(sb.ToString(), sqlParameter));

                if (id > 0)
                {
                    news_Detail.news_id = id;

                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                throw;
            }

        }
        
        public bool News_Detail_AddBatch(List<News_Detail> list)
        {
            List<SQLEntity> sqlEntity = new List<SQLEntity>();

            foreach (var item in list)
            {
                var single = new SQLEntity()
                {
                    DicEntry = News_Detail_GetParameter_Add(item),
                    obj = item
                };

                sqlEntity.Add(single);
            };

            DbHelperSQL.ExecuteSqlTran(sqlEntity);

            foreach (var item in sqlEntity)
            {
                var entity = item.obj as News_Detail;

                entity.news_id = Convert.ToInt32(item.ObjIdentity);

                entity.entityState = EntityState.UnChange;
            }

            return true;
        }
        
        
        public bool News_Detail_AddOrUpdate(News_Detail news_Detail)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("replace into [News_Detail] ");
            sb.Append("(");
            sb.Append("news_id,");
            sb.Append("title,");
            sb.Append("content,");
            sb.Append("disable,");
            sb.Append("create_time,");
            sb.Append("last_modify_time");
            sb.Append(") ");
            sb.Append("values");
            sb.Append(" (");
            sb.Append("@news_id,");
            sb.Append("@title,");
            sb.Append("@content,");
            sb.Append("@disable,");
            sb.Append("@create_time,");
            sb.Append("@last_modify_time");
           sb.Append(") ;select last_insert_rowid()");

            SQLiteParameter[] sqlParameter = new SQLiteParameter[]
            {
                   new SQLiteParameter("@news_id",news_Detail.news_id),
                   new SQLiteParameter("@title",news_Detail.title),
                   new SQLiteParameter("@content",news_Detail.content),
                   new SQLiteParameter("@disable",news_Detail.disable),
                   new SQLiteParameter("@create_time",news_Detail.create_time),
                   new SQLiteParameter("@last_modify_time",news_Detail.last_modify_time),
            };

            try
            {
                int id = Convert.ToInt32(DbHelperSQL.ExecuteScalar(sb.ToString(), sqlParameter));

                if (id > 0)
                {
                    news_Detail.news_id = id;

                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                throw;
            }

        }

        public bool News_Detail_Update(News_Detail news_Detail)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("update [News_Detail] set ");
            sb.Append("news_id=@news_id,");
            sb.Append("title=@title,");
            sb.Append("content=@content,");
            sb.Append("disable=@disable,");
            sb.Append("create_time=@create_time,");
            sb.Append("last_modify_time=@last_modify_time");
            sb.Append(" where news_id=@news_id");
            
            SQLiteParameter[] parameters = 
            {
                  new SQLiteParameter("@news_id",news_Detail.news_id),
                  new SQLiteParameter("@title",news_Detail.title),
                  new SQLiteParameter("@content",news_Detail.content),
                  new SQLiteParameter("@disable",news_Detail.disable),
                  new SQLiteParameter("@create_time",news_Detail.create_time),
                  new SQLiteParameter("@last_modify_time",news_Detail.last_modify_time),
            };

            try
            {
                int rows = DbHelperSQL.ExecuteSql(sb.ToString(), parameters);

                if (rows > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        
        public bool News_Detail_UpdateBatch(List<News_Detail> list)
        {
            Hashtable sqlHash = new Hashtable();

            foreach (var item in list)
            {
             StringBuilder sb = new StringBuilder();

             sb.Append("update [News_Detail] set ");
             sb.Append("title=@title,");
             sb.Append("content=@content,");
             sb.Append("disable=@disable,");
             sb.Append("create_time=@create_time,");
             sb.Append("last_modify_time=@last_modify_time");
             sb.Append(" where news_id="+item.news_id);
            
             SQLiteParameter[] sqlParameter = new SQLiteParameter[]
            {
                   new SQLiteParameter("@title",item.title),
                   new SQLiteParameter("@content",item.content),
                   new SQLiteParameter("@disable",item.disable),
                   new SQLiteParameter("@create_time",item.create_time),
                   new SQLiteParameter("@last_modify_time",item.last_modify_time),
            };

            if (!sqlHash.ContainsKey(sb.ToString()))
             {
                  sqlHash.Add(sb.ToString(), sqlParameter);
             }
            }
            try
            {
                DbHelperSQL.ExecuteSqlTran(sqlHash);

                return true;
            }
            catch (Exception ex)
            {
                throw;
            } 
        }

        public bool News_Detail_Remove(News_Detail news_Detail)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from [News_Detail] ");
            sb.Append(" where news_id=@news_id");
            SQLiteParameter[] parameters =
            {
                new SQLiteParameter("@news_id",news_Detail.news_id)
            };

            try
            {
              int rows = DbHelperSQL.ExecuteSql(sb.ToString(), parameters);

                if (rows > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        
        public bool News_Detail_RemoveFromField(string field, object fieldValue)
        {

            StringBuilder sb = new StringBuilder();

            sb.Append("delete from [News_Detail] ");
            sb.AppendFormat(" where {0} ='{1}'", field, fieldValue);

            try
            {
                int rows = DbHelperSQL.ExecuteSql(sb.ToString());

                if (rows > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        
        /// <summary>
        /// ids必须已逗号隔开
        /// </summary>
        /// <param name="ids"></param>
        public bool News_Detail_RemoveList(string ids)
        {
            if (string.IsNullOrEmpty(ids))
                return true;

            StringBuilder sb = new StringBuilder();

            sb.Append("delete from [News_Detail] ");
            sb.Append(" where news_id in (" + ids + ")");

            try
            {
                int rows = DbHelperSQL.ExecuteSql(sb.ToString());

                if (rows > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        
        public bool News_Detail_RemoveList(List<int> ids)
        {
            if (ids.Count <= 0)
                return true;
            List<string> sql = new List<string>();
            StringBuilder sb = new StringBuilder();
            foreach (var i in ids)
            {
                sb.Clear();
                sb.Append("delete from [News_Detail] ");
                sb.AppendFormat(" where news_id ={0}", i);
                sql.Add(sb.ToString());
            }
            try
            {
                int rows = DbHelperSQL.ExecuteSqlTran(sql);

                if (rows > 0)
                    return true;
                return false;
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}
